package GUI;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import logic.Server;

public class MainPanel extends JPanel{

	/**
	 * 
	 */
	private Server server;
	private static final long serialVersionUID = 1L;
	

	
	public MainPanel(final Server server) {
		;
		setLayout(null);
		setServer(server);
		JLabel lblDistributedBackupService = new JLabel("Distributed Backup Service");
		lblDistributedBackupService.setBounds(166, 31, 209, 22);
		lblDistributedBackupService.setFont(new Font("Tahoma", Font.PLAIN, 18));
		add(lblDistributedBackupService);
		
		JButton btnNewBackup = new JButton("New Backup");
		btnNewBackup.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		btnNewBackup.setBounds(125, 103, 294, 22);
		btnNewBackup.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {
				JFrame chooserFrame = new JFrame();
				final JFileChooser fc = new JFileChooser();
				int returnVal = fc.showOpenDialog(chooserFrame);
				 
	            if (returnVal == JFileChooser.APPROVE_OPTION) {
	                File file = fc.getSelectedFile();
	                
	                BackupDialog b_dialog = new BackupDialog(file,server);
	                
	                b_dialog.setModal(true);  
	                b_dialog.pack();  
	                b_dialog.setVisible(true);  
	                
	            } else {
	            }
	           
			}
		});
		add(btnNewBackup);
		
		JButton btnRestoreFile = new JButton("Restore File");
	
		btnRestoreFile.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {
				GUI.frame.setContentPane(new RestorePanel(server));

				((JPanel)GUI.frame.getContentPane()).revalidate();
				GUI.frame.repaint();
			}
		});
		btnRestoreFile.setBounds(125, 136, 294, 23);
		add(btnRestoreFile);
		
		JButton btnDeleteFile = new JButton("Delete File");
		btnDeleteFile.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {
				GUI.frame.setContentPane(new DeletePanel(server));
				((JPanel)GUI.frame.getContentPane()).revalidate();
				GUI.frame.repaint();
			}
		});
		btnDeleteFile.setBounds(125, 170, 294, 23);
		add(btnDeleteFile);
		
		JButton btnReclaimSpace = new JButton("Reclaim Space");
		btnReclaimSpace.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				GUI.frame.setContentPane(new ReclaimPanel(server));
				((JPanel)GUI.frame.getContentPane()).revalidate();
				GUI.frame.repaint();
			}
		});
		btnReclaimSpace.setBounds(125, 204, 294, 23);
		add(btnReclaimSpace);
		
		JLabel lblLog = new JLabel("Log");
		lblLog.setBounds(23, 273, 46, 14);
		add(lblLog);
		
		JButton btnExitButton = new JButton("Exit");
		btnExitButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {
				try {
					server.exit();
				} catch (Exception e) {
					 
					//e.printStackTrace();
				}
				System.exit(0);
			}
		});
		btnExitButton.setBounds(125, 238, 294, 23);
		add(btnExitButton);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 298, 515, 132);
		add(scrollPane);
		
		JList<String> list = new JList<String>(Server.logMensagesStr);
		scrollPane.setViewportView(list);
		list.setValueIsAdjusting(true);
	}

	public Server getServer() {
		return server;
	}

	public void setServer(Server server) {
		this.server = server;
	}
}
